using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ByHand.Model;
using ByHand.Data;
using ByHand.Services.Interfaces;
using ByHand.Data.Interfaces;
using ByHand.Services.Code;

namespace ByHand.Services.Implements
{
    public class OrderService : RepositoryBase<Order>, IOrderService
    {
        public OrderService(IDatabaseFactory DbFactory) : base(DbFactory) { }
        public List<Order> GetByProduct(int productID)
        {
            return (from o in ByHandDB.Orders
                    where o.ProductID == productID
                    orderby o.BoughtDate descending
                    select o).ToList();
        }
        public List<Order> GetByPage(int page, int pagesize, out int total)
        {
            var lstO = from o in ByHandDB.Orders
                       orderby o.BoughtDate descending
                       select o;
            total = lstO.Count();
            return lstO.Skip((page - 1) * pagesize).Take(pagesize).ToList();
        }
        public List<Order> GetByPageFilter(int page, int pagesize, out int total, string filter)
        {
            var lstO = from o in ByHandDB.Orders
                       where o.Buyer.Contains(filter) || o.ToName.Contains(filter)
                       orderby o.BoughtDate descending
                       select o;
            total = lstO.Count();
            return lstO.Skip((page - 1) * pagesize).Take(pagesize).ToList();
        }
        public List<Order> GetByStatus(int page, int pagesize, out int total, int status) {
            var lstO = from o in ByHandDB.Orders
                       where o.Status == status
                       orderby o.BoughtDate descending
                       select o;
            total = lstO.Count();
            return lstO.Skip((page - 1) * pagesize).Take(pagesize).ToList();
        }
        public List<Order> GetByStatusFilter(int page, int pagesize, out int total, string filter, int status) {
            var lstO = from o in ByHandDB.Orders
                       where (o.Buyer.Contains(filter) || o.ToName.Contains(filter)) && o.Status == status
                       orderby o.BoughtDate descending
                       select o;
            total = lstO.Count();
            return lstO.Skip((page - 1) * pagesize).Take(pagesize).ToList();
        }
    }
}
